#!/bin/bash
sudo apt-get -y install dos2unix
echo "begin--------------------upgrade------------------"
binpath=/home/linaro/work/edge/bin
backpath=/home/linaro/work/edge/bin_back

tmp=/home/linaro/work/edge/tmp
rm -fr $backpath
df -h >> ../display.txt
echo "begin--------------------upgrade-------5%-----------" >> ../display.txt
mkdir $backpath
allfile=$binpath"/*"
cp -r  $allfile $backpath
echo "begin--------------------upgrade-------35%-----------" >> ../display.txt

echo "----------------------------------------------" >> ../display.txt
echo "----------------------------------------------" >> ../display.txt
echo "----------------------------------------------" >> ../display.txt
ls -lht $backpath  >> ../display.txt
echo "------------old----version------------------------------" >> ../display.txt
echo "------------old----version------------------------------" >> ../display.txt
cd $backpath
cat ver >> ../display.txe
echo "$1"
echo "begin-----------tar file to ---------upgrade-------38%-----------" >> ../display.txt
#echo $1 >> ../display.txt
tar -zxvf $1 -C $backpath >> ../display.txt
#echo "now show version file" >> ../display.txt
#echo $backpath/ver >> ../display.txt
echo "------------new----version------------------------------" >> ../display.txt
echo "------------new----version------------------------------" >> ../display.txt
cd $backpath
cat ver >> ../display.txt
echo "begin--------------------upgrade-------50%-----------" >> ../display.txt
#ls $backpath >> ../display.txt
#echo "begin--------------------upgrade-------75%-----------"  >> ../display.txt

cd $backpath
bash ./before.sh
echo "begin--------------------upgrade-------85%-----------"  >> ../display.txt
#echo $backpath 1>>../display.txt
#echo $tmp
#echo $binpath

sudo service cron stop
cd $binpath
#echo "nowbinpath:" >> ../display.txt
#ls >> ../display.txt
#echo "stop--------------------------------------------------------------------------------------"  >> ../display.txt
bash ./stop.sh
echo "stop--------------------------------------------------finished-----------"
cd ..
mv $backpath $tmp
mv $binpath $backpath
mv $tmp $binpath
cd $binpath
dos2unix *.sh
chmod 777 *.sh
chmod 777 e*
#echo "bin dir" >> ../display.txt
#ls  >> ../display.txt
cat ver >> ../display.txt

bash $binpath/after.sh
echo "runnging------------------upgrade-------90%-----------"  >> ../display.txt
bash $binpath/healthy.sh >> ../display.txt
echo "running------------------upgrade-------95%-----------"   >> ../display.txt
echo $2 | sudo -S service cron start
echo "END----------------------upgrade-------100%-----------"   >> ../display.txt
echo "END----------------------upgrade-------100%-----------"

